//
//  AutoTimer.m
//  BOX
//
//  Created by 许杨 on 2017/5/13.
//  Copyright © 2017年 XY. All rights reserved.
//

#import "AutoTimer.h"

@implementation AutoTimer

/**
 单例
 */
// 创建静态对象 防止外部访问
static AutoTimer *_instance;

//单例模式
+(AutoTimer*)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[AutoTimer alloc]init];
        _instance.isRun = NO;
        _instance.NowTime = 60;
    });
    return _instance;
}

- (void)AuotoStart{
    if (self.isRun) {
        return;
    }
    self.NowTime = 60;
    self.isRun = YES;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime:) userInfo:nil repeats:60];
}

- (void)changeTime:(NSTimer*)timer{
    self.NowTime--;
  
    if (self.NowTime == 0) {
        self.isRun = NO;
        [timer invalidate];
        timer = nil;
    }
}

- (void)resetTimer{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.isRun = NO;
}
@end
